package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.UserUnDoVo;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserDevelopMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发展党员基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class SysUserDevelopServiceImpl implements ISysUserDevelopService
{
    @Autowired
    private SysUserDevelopMapper sysUserDevelopMapper;
    @Autowired
    private ISysUserStage1Service sysUserStage1Service;
    @Autowired
    private ISysUserStage2Service sysUserStage2Service;
    @Autowired
    private ISysUserStage3Service sysUserStage3Service;
    @Autowired
    private ISysUserStage4Service sysUserStage4Service;
    @Autowired
    private ISysUserStage5Service sysUserStage5Service;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;
    /**
     * 查询发展党员基本信息
     *
     * @param id 发展党员基本信息主键
     * @return 发展党员基本信息
     */
    @Override
    public SysUserDevelop selectSysUserDevelopById(Long id)
    {
        SysUserDevelop sysUserDevelop = sysUserDevelopMapper.selectSysUserDevelopById(id);
        SysUserStage1 sysUserStage1 = sysUserStage1Service.selectSysUserStage1ByDevelopId(id);
        sysUserDevelop.setSysUserStage1(sysUserStage1);
        SysUserStage2 sysUserStage2 = sysUserStage2Service.selectSysUserStage2ByDevelopId(id);
        sysUserDevelop.setSysUserStage2(sysUserStage2);
        SysUserStage3 sysUserStage3 = sysUserStage3Service.selectSysUserStage3ByDevelopId(id);
        sysUserDevelop.setSysUserStage3(sysUserStage3);
        SysUserStage4 sysUserStage4= sysUserStage4Service.selectSysUserStage4ByDevelopId(id);
        sysUserDevelop.setSysUserStage4(sysUserStage4);
        SysUserStage5 sysUserStage5 = sysUserStage5Service.selectSysUserStage5ByDevelopId(id);
        sysUserDevelop.setSysUserStage5(sysUserStage5);
        return sysUserDevelop;
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param sysUserDevelop 用户信息
     * @return 结果
     */
    @Override
    public boolean checkPhoneUnique(SysUserDevelop sysUserDevelop){
        Long id = StringUtils.isNull(sysUserDevelop.getId()) ? -1L : sysUserDevelop.getId();
        SysUserDevelop info = sysUserDevelopMapper.checkPhoneUnique(sysUserDevelop.getPhonenumber());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询发展党员基本信息列表
     *
     * @param sysUserDevelop 发展党员基本信息
     * @return 发展党员基本信息
     */
    @Override
    public List<SysUserDevelop> selectSysUserDevelopList(SysUserDevelop sysUserDevelop)
    {
        return sysUserDevelopMapper.selectSysUserDevelopList(sysUserDevelop);
    }

    /**
     * 新增发展党员基本信息
     *
     * @param sysUserDevelop 发展党员基本信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysUserDevelop(SysUserDevelop sysUserDevelop)
    {
        sysUserDevelop.setCreateTime(DateUtils.getNowDate());
        sysUserDevelop.setStatus("0");
        sysUserDevelop.setDevelopStage("提交入党申请书");
        int i = sysUserDevelopMapper.insertSysUserDevelop(sysUserDevelop);
        SysUserStage1 sysUserStage1 = new SysUserStage1();
        sysUserStage1.setSysUserDevelopId(sysUserDevelop.getId());
        sysUserStage1.setCreateBy(sysUserDevelop.getCreateBy());
        sysUserStage1Service.insertSysUserStage1(sysUserStage1);
        SysUserStage2 sysUserStage2 = new SysUserStage2();
        sysUserStage2.setSysUserDevelopId(sysUserDevelop.getId());
        sysUserStage2.setCreateBy(sysUserDevelop.getCreateBy());
        sysUserStage2Service.insertSysUserStage2(sysUserStage2);
        SysUserStage3 sysUserStage3 = new SysUserStage3();
        sysUserStage3.setSysUserDevelopId(sysUserDevelop.getId());
        sysUserStage3.setCreateBy(sysUserDevelop.getCreateBy());
        sysUserStage3Service.insertSysUserStage3(sysUserStage3);
        SysUserStage4 sysUserStage4 = new SysUserStage4();
        sysUserStage4.setSysUserDevelopId(sysUserDevelop.getId());
        sysUserStage4.setCreateBy(sysUserDevelop.getCreateBy());
        sysUserStage4Service.insertSysUserStage4(sysUserStage4);
        SysUserStage5 sysUserStage5 = new SysUserStage5();
        sysUserStage5.setSysUserDevelopId(sysUserDevelop.getId());
        sysUserStage5.setCreateBy(sysUserDevelop.getCreateBy());
        sysUserStage5Service.insertSysUserStage5(sysUserStage5);
        //创建发展党员归档文件夹
        DeptFileArchived deptFileArchived = new DeptFileArchived();
        deptFileArchived.setParentId(4L);
        deptFileArchived.setDeptId(sysUserDevelop.getDeptId());
        deptFileArchived.setFileName(sysUserDevelop.getNickName());
        deptFileArchived.setFileType("0");
        deptFileArchived.setCreateBy(sysUserDevelop.getCreateBy());
        deptFileArchived.setAssociationId(String.valueOf(sysUserDevelop.getId()));
        SysUser user = sysUserService.selectUserById(Long.valueOf(sysUserDevelop.getCreateBy()));
        deptFileArchived.setCreateName(user.getNickName());
        deptFileArchivedService.insertDeptFileArchived(deptFileArchived);
        return i;
    }

    /**
     * 修改发展党员基本信息
     *
     * @param sysUserDevelop 发展党员基本信息
     * @return 结果
     */
    @Override
    public int updateSysUserDevelop(SysUserDevelop sysUserDevelop)
    {
        sysUserDevelop.setUpdateTime(DateUtils.getNowDate());
        return sysUserDevelopMapper.updateSysUserDevelop(sysUserDevelop);
    }

    /**
     * 删除发展党员基本信息信息
     *
     * @param id 发展党员基本信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSysUserDevelopById(Long id)
    {
        int i = sysUserDevelopMapper.deleteSysUserDevelopById(id);
        SysUserDevelop sysUserDevelop = sysUserDevelopMapper.selectSysUserDevelopById(id);
        Long userDevelopId = sysUserDevelop.getId();
        sysUserStage1Service.deleteSysUserStage1ByUserDevelopId(userDevelopId);
        sysUserStage2Service.deleteSysUserStage2ByUserDevelopId(userDevelopId);
        sysUserStage3Service.deleteSysUserStage3ByUserDevelopId(userDevelopId);
        sysUserStage4Service.deleteSysUserStage4ByUserDevelopId(userDevelopId);
        sysUserStage5Service.deleteSysUserStage5ByUserDevelopId(userDevelopId);
        return i;
    }


    /**
     * 发展党员谈话待办任务
     *
     */
    @Override
    public List<SysUserStage1> selectTalkTask(UserUnDoVo userUnDoVo){
        return sysUserDevelopMapper.selectTalkTask(userUnDoVo);
    }
}
